#include "home.h"

home::home(QWidget *parent) : QWidget(parent)
{
    this->setStyleSheet("background-color:#EEEEEE;");
    init();
    styleInit();

}
//布局初始化
void home::init()
{
    // 主布局 垂直
    mainLayout = new QVBoxLayout(this); // 将 mainLayout 直接添加到 testhome 窗口

    // 头部布局
    TopLayout = new QHBoxLayout;

    // 四个顶部组
    Top1GroupBox = new QGroupBox(this); // 明确父部件为 testhome 窗口
    Top2GroupBox = new QGroupBox(this);
    Top3GroupBox = new QGroupBox(this);
    Top4GroupBox = new QGroupBox(this);

    // 四个顶部水平容器
    Top1Layout = new QHBoxLayout(Top1GroupBox);
    Top2Layout = new QHBoxLayout(Top2GroupBox);
    Top3Layout = new QHBoxLayout(Top3GroupBox);
    Top4Layout = new QHBoxLayout(Top4GroupBox);

    // 四个顶部各有的垂直容器
    Top1VLayout = new QVBoxLayout;
    Top2VLayout = new QVBoxLayout;
    Top3VLayout = new QVBoxLayout;
    Top4VLayout = new QVBoxLayout;

    // 文字
    Top1_1Label = new QLabel("今日挂号人数", this);
    Top1_2Label = new QLabel("266人", this);
    Top1_3Label = new QLabel("较昨日减少20人", this);

    Top2_1Label = new QLabel("今日退号人数", this);
    Top2_2Label = new QLabel("266人", this);
    Top2_3Label = new QLabel("较昨日减少20人", this);

    Top3_1Label = new QLabel("今建档人数", this);
    Top3_2Label = new QLabel("266人", this);
    Top3_3Label = new QLabel("较昨日减少20人", this);

    Top4_1Label = new QLabel("今日退档人数", this);
    Top4_2Label = new QLabel("266人", this);
    Top4_3Label = new QLabel("较昨日减少20人", this);

    // 按钮
    Top1_1 = new QPushButton("查看详情", this);
    Top1_2 = new QPushButton("", this);
    Top1_2->setFixedSize(50, 50);

    Top2_1 = new QPushButton("查看详情", this);
    Top2_2 = new QPushButton("", this);
    Top2_2->setFixedSize(50, 50);

    Top3_1 = new QPushButton("查看详情", this);
    Top3_2 = new QPushButton("", this);
    Top3_2->setFixedSize(50, 50);

    Top4_1 = new QPushButton("查看详情", this);
    Top4_2 = new QPushButton("", this);
    Top4_2->setFixedSize(50, 50);

    //给图片按钮 和 文字添加到一个容器 保持平行
    QHBoxLayout *top1_2Layout = new QHBoxLayout;
    top1_2Layout->addWidget(Top1_2Label);
    top1_2Layout->addWidget(Top1_2);

    QHBoxLayout *top2_2Layout = new QHBoxLayout;
    top2_2Layout->addWidget(Top2_2Label);
    top2_2Layout->addWidget(Top2_2);

    QHBoxLayout *top3_2Layout = new QHBoxLayout;
    top3_2Layout->addWidget(Top3_2Label);
    top3_2Layout->addWidget(Top3_2);

    QHBoxLayout *top4_2Layout = new QHBoxLayout;
    top4_2Layout->addWidget(Top4_2Label);
    top4_2Layout->addWidget(Top4_2);

    //分别添加进去
    Top1VLayout->addWidget(Top1_1Label);
    Top1VLayout->addLayout(top1_2Layout);
    Top1VLayout->addWidget(Top1_3Label);
    Top1VLayout->addWidget(Top1_1);
    Top1Layout->addLayout(Top1VLayout);

    Top2VLayout->addWidget(Top2_1Label);
    Top2VLayout->addLayout(top2_2Layout);
    Top2VLayout->addWidget(Top2_3Label);
    Top2VLayout->addWidget(Top2_1);
    Top2Layout->addLayout(Top2VLayout);


    Top3VLayout->addWidget(Top3_1Label);
    Top3VLayout->addLayout(top3_2Layout);
    Top3VLayout->addWidget(Top3_3Label);
    Top3VLayout->addWidget(Top3_1);
    Top3Layout->addLayout(Top3VLayout);


    Top4VLayout->addWidget(Top4_1Label);
    Top4VLayout->addLayout(top4_2Layout);
    Top4VLayout->addWidget(Top4_3Label);
    Top4VLayout->addWidget(Top4_1);
    Top4Layout->addLayout(Top4VLayout);


    // 设置拉伸因子，使四个顶部组平均分配水平空间
    // 调整添加部件的顺序，先添加 Top4GroupBox
    TopLayout->addWidget(Top1GroupBox);
    TopLayout->addWidget(Top2GroupBox);
    TopLayout->addWidget(Top3GroupBox);
    TopLayout->addWidget(Top4GroupBox);

    TopLayout->setStretch(0, 1);
    TopLayout->setStretch(1, 1);
    TopLayout->setStretch(2, 1);
    TopLayout->setStretch(3, 1);


    Top1GroupBox->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
    Top2GroupBox->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
    Top3GroupBox->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
    Top4GroupBox->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);

    // 为按钮设置更灵活的尺寸策略
    Top1_2->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
    Top2_2->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
    Top3_2->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
    Top4_2->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);

    mainLayout->addLayout(TopLayout);


    //----------------------------------------------------------------------------------------------------


    // 新的水平容器
    QHBoxLayout *newHorizontalLayout = new QHBoxLayout;
    mainLayout->addLayout(newHorizontalLayout);

    // 新的两个组容器
    leftGroupBox = new QGroupBox(this);
    rightGroupBox = new QGroupBox(this);

    QVBoxLayout *leftLayout = new QVBoxLayout(leftGroupBox);
    QVBoxLayout *rightLayout = new QVBoxLayout(rightGroupBox);

    // 先创建并添加 chartView 到布局中
    chartInit();
    leftLayout->addWidget(chartView);

    newHorizontalLayout->addWidget(leftGroupBox);
    newHorizontalLayout->addWidget(rightGroupBox);

    // 设置尺寸策略
    leftGroupBox->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
    rightGroupBox->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);

    //-----------------------------------------------------------------------------------
    QLabel *rightLabel = new QLabel("📢 公告栏目");
    rightLabel->setMaximumHeight(30);
    rightLabel->setStyleSheet(
                    "color:red;"
                    "font: 15pt '等线';"
                );


    //右半边的布局
    QWidget * rightDownWidget = new QWidget;
    rightDownWidget->setStyleSheet("background-color:#FFFAF0;");
    rightDownWidget->setMinimumHeight(400);
    QVBoxLayout *rightDownLayout = new QVBoxLayout(rightDownWidget);


    // 创建一个 QFrame 作为水平线
    QFrame *horizontalLine = new QFrame();
    // 设置 QFrame 的形状为水平线
    horizontalLine->setFrameShape(QFrame::HLine);
    // 设置 QFrame 的阴影样式，这里使用的是普通平面样式
    horizontalLine->setFrameShadow(QFrame::Plain);
    // 设置 QFrame 的线宽，可根据需要调整
    horizontalLine->setLineWidth(1);
    horizontalLine->setStyleSheet("background-color:black;");

    rightDownLayout->addWidget(horizontalLine);



    rightLayout->addWidget(rightLabel);
    rightLayout->addWidget(rightDownWidget);







    // 更新布局和显示
    this->update();
}
// 样式初始化
void home::styleInit()
{
    Top1GroupBox->setStyleSheet("border-radius:12px;background-color:#FFFACD;");
    Top2GroupBox->setStyleSheet("border-radius:12px;background-color:#ccffff;");
    Top3GroupBox->setStyleSheet("border-radius:12px;background-color:#99ffcc;");
    Top4GroupBox->setStyleSheet("border-radius:12px;background-color:#EED2EE;");

    leftGroupBox->setStyleSheet("border-radius:12px;background-color:rgb(18,168,231);");
    rightGroupBox->setStyleSheet("border-radius:12px;background-color:#FFDEAD;");


    Top1_1->setStyleSheet("QPushButton {"
                              "border: none;"
                              "text-align:left;"
                          "}"
                          "QPushButton:hover{"
                              "color:blue;"
                          "}"
                          );
    Top2_1->setStyleSheet("QPushButton {"
                              "border: none;"
                              "text-align:left;"
                          "}"
                          "QPushButton:hover{"
                              "color:blue;"
                          "}"
                          );

    Top3_1->setStyleSheet("QPushButton {"
                              "border: none;"
                              "text-align:left;"
                          "}"
                          "QPushButton:hover{"
                              "color:blue;"
                          "}"
                          );

    Top4_1->setStyleSheet("QPushButton {"
                              "border: none;"
                              "text-align:left;"
                          "}"
                          "QPushButton:hover{"
                              "color:blue;"
                          "}"
                          );

    Top1_2->setStyleSheet(
        "QPushButton {"
            "border: none;"
            "background-repeat:no-repeat;"
            "background-image: url(:/res/registPress.svg);"
            "background-size: contain;"
            "background-position: center center;"
            "padding: 0px;"
        "}"
        "QPushButton:pressed {"
            "border: none;"
            "background-image: url(:/res/registRelease.svg);"
            "background-size: contain;"
            "background-position: center center;"
            "padding: 0px;"
        "}"
        "QPushButton:releas {"
            "background-image: url(:/res/registPress.svg);"
            "background-size: contain;"
            "background-position: center center;"
            "padding: 0px;"
        "}"
    );
    // 统一设置Top2_2按钮及相关状态样式
    Top2_2->setStyleSheet(
        "QPushButton {"
            "border: none;"
            "background-repeat:no-repeat;"
            "background-image: url(:/res/RefundPress.svg);"
            "background-size: contain;"
            "background-position: center center;"
            "padding: 0px;"
        "}"
        "QPushButton:pressed {"
            "border: none;"
            "background-image: url(:/res/RefundRelease.svg);"
            "background-size: contain;"
            "background-position: center center;"
            "padding: 0px;"
        "}"
        "QPushButton:releas {"
            "background-image: url(:/res/RefundPress.svg);"
            "background-size: contain;"
            "background-position: center center;"
            "padding: 0px;"
        "}"
    );
    // 统一设置Top3_2按钮及相关状态样式
    Top3_2->setStyleSheet(
        "QPushButton {"
            "border: none;"
            "background-repeat:no-repeat;"
            "background-image: url(:/res/DocumentationPress.svg);"
            "background-size: contain;"
            "background-position: center center;"
            "padding: 0px;"
        "}"
        "QPushButton:pressed {"
            "border: none;"
            "background-image: url(:/res/DocumentationRelease.svg);"
            "background-size: contain;"
            "background-position: center center;"
            "padding: 0px;"
        "}"
        "QPushButton:releas {"
            "background-image: url(:/res/DocumentationPress.svg);"
            "background-size: contain;"
            "background-position: center center;"
            "padding: 0px;"
        "}"
    );
    // 统一设置Top4_2按钮及相关状态样式
    Top4_2->setStyleSheet(
        "QPushButton {"
            "border: none;"
            "background-repeat:no-repeat;"
            "background-image: url(:/res/BackshiftPress.svg);"
            "background-size: contain;"
            "background-position: center center;"
            "padding: 0px;"
        "}"
        "QPushButton:pressed {"
            "border: none;"
            "background-image: url(:/res/BackshiftRelease.svg);"
            "background-size: contain;"
            "background-position: center center;"
            "padding: 0px;"
        "}"
        "QPushButton:releas {"
            "background-image: url(:/res/BackshiftPress.svg);"
            "background-size: contain;"
            "background-position: center center;"
            "padding: 0px;"
        "}"
    );

    // 统一设置各标签的样式
    Top1_1Label->setStyleSheet(
        "color:#44464C;"
        "font: 14pt '等线';"
        "word-wrap: false;"
    );
    Top2_1Label->setStyleSheet(
        "color:#44464C;"
        "font: 14pt '等线';"
        "word-wrap: false;"
    );
    Top3_1Label->setStyleSheet(
        "color:#44464C;"
        "font: 14pt '等线';"
        "word-wrap: false;"
    );
    Top4_1Label->setStyleSheet(
        "color:#44464C;"
        "font: 14pt '等线';"
        "word-wrap: false;"
    );

    Top1_2Label->setStyleSheet(
        "color:black;"
        "font: 75 19pt 'Arial';"
        "word-wrap: false;"
    );
    Top2_2Label->setStyleSheet(
        "color:black;"
        "font: 75 19pt 'Arial';"
        "word-wrap: false;"
    );
    Top3_2Label->setStyleSheet(
        "color:black;"
        "font: 75 19pt 'Arial';"
        "word-wrap: false;"
    );
    Top4_2Label->setStyleSheet(
        "color:black;"
        "font: 75 19pt 'Arial';"
        "word-wrap: false;"
    );

    Top1_3Label->setStyleSheet(
        "color:#44464C;"
        "font: 12pt '等线';"
        "word-wrap: false;"
    );
    Top2_3Label->setStyleSheet(
        "color:#44464C;"
        "font: 12pt '等线';"
        "word-wrap: false;"
    );
    Top3_3Label->setStyleSheet(
        "color:#44464C;"
        "font: 12pt '等线';"
        "word-wrap: false;"
    );
    Top4_3Label->setStyleSheet(
        "color:#44464C;"
        "font: 12pt '等线';"
        "word-wrap: false;"
    );


}
// 图表初始化
void home::chartInit()
{
    //雷达图
    //QScatterSeries *lineSeries = new QScatterSeries();
    //折线
//    QLineSeries *series = new QLineSeries();

    // 创建一个 QLineSeries 用于存储数据
    QLineSeries *series = new QLineSeries();
    // 添加数据点的部分，最后的值是 折线图中的数据
    series->append(QDateTime::fromString("2024-01-01", "yyyy-MM-dd").toMSecsSinceEpoch(), 150);
    series->append(QDateTime::fromString("2024-01-02", "yyyy-MM-dd").toMSecsSinceEpoch(), 87);
    series->append(QDateTime::fromString("2024-01-03", "yyyy-MM-dd").toMSecsSinceEpoch(), 116);
    series->append(QDateTime::fromString("2024-01-04", "yyyy-MM-dd").toMSecsSinceEpoch(), 66);
    series->append(QDateTime::fromString("2024-01-05", "yyyy-MM-dd").toMSecsSinceEpoch(), 120);


    // 创建一个 QChart 对象并添加系列
    QChart *chart = new QChart();
    chart->addSeries(series);
    // 设置图表的标题
    chart->setTitle("<h1 style='color:rgb(18,150,219);text-align:left;'>挂号人数趋势图</h1>");
    // 创建并设置 X 轴为 QDateTimeAxis
    QDateTimeAxis *axisX = new QDateTimeAxis();
    axisX->setTitleText("日期");

    // 设置 X 轴标题字体大小
    QFont axisXTitleFont;
    axisXTitleFont.setPointSize(15);
    axisX->setTitleFont(axisXTitleFont);

    axisX->setFormat("yyyy-MM-dd");
    axisX->setTickCount(5);
    chart->addAxis(axisX, Qt::AlignBottom);
    series->attachAxis(axisX);

    // 创建并设置 Y 轴为 QValueAxis
    QValueAxis *axisY = new QValueAxis();
    axisY->setTitleText("人数");
    // 设置 Y 轴标题字体大小
    QFont axisYTitleFont;
    axisYTitleFont.setPointSize(10);
    axisY->setTitleFont(axisYTitleFont);

    axisY->setTickCount(7);
    axisY->setRange(0, 200);
    chart->addAxis(axisY, Qt::AlignLeft);
    series->attachAxis(axisY);
    axisY->setLabelsAngle(0);
    // 创建一个 QChartView 用于显示图表
    chartView = new QChartView(chart);
    chartView->setStyleSheet("background-color:white;");
//    chartView->setMinimumHeight(460);
    chartView->setRenderHint(QPainter::Antialiasing);
    chartView->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
}


// 重写 resizeEvent 函数
void home::resizeEvent(QResizeEvent *event)
{
    QWidget::resizeEvent(event);
    int newHeight = this->height();
    int newWidth = this->width();

    // 计算顶部容器的新高度
    int topContainerHeight = newHeight / 4;

    // 设置四个顶部组的高度
    Top1GroupBox->setFixedHeight(topContainerHeight);
    Top2GroupBox->setFixedHeight(topContainerHeight);
    Top3GroupBox->setFixedHeight(topContainerHeight);
    Top4GroupBox->setFixedHeight(topContainerHeight);

    // 设置拉伸因子，使四个顶部组平均分配水平空间
    TopLayout->setStretch(0, 1);
    TopLayout->setStretch(1, 1);
    TopLayout->setStretch(2, 1);
    TopLayout->setStretch(3, 1);

    // 获取新的水平容器
    QLayoutItem *item = mainLayout->itemAt(1);
    if (item) {
        QHBoxLayout *newHorizontalLayout = qobject_cast<QHBoxLayout*>(item->layout());
        if (newHorizontalLayout) {
            // 设置新的水平容器中组容器的拉伸因子
            newHorizontalLayout->setStretch(0, 3);
            newHorizontalLayout->setStretch(1, 1);
        }
    }
}

